<!--
 * @Author: 郭安东
 * @Date: 2021-03-12 16:51:04
 * @LastEditTime: 2021-04-12 15:23:57
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \test\src\views\function\temperature.vue
-->
<template>
  <div class="temperature">
    <el-card class="box-card">
      欢迎新用户:<el-link class="userName">{{ userName }}</el-link>登入系统
    </el-card>
    <four-menu :buts-data="dataFourMenu" :title=" '基础信息填报' " class="four-menu"/>
    <div>
      <el-card class="box-card">
        <div class="form-box">
          <el-form ref="form" :model="form" label-width="160px">
            <el-form-item label="姓名" required>
              <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="社区" required>
              <el-select v-model="optionCommunity.value" placeholder="请选择" style="width: 340px" @change="getEstate(optionCommunity.value)">
                <el-option
                  v-for="item in optionCommunity"
                  :key="item.value"
                  :value="item.value"
                  :label="item.label">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="小区" required>
              <el-select v-model="form.estate" placeholder="请选择" style="width: 340px">
                <el-option
                  v-for="item in optionEstate"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="房号" required>
              <el-input v-model="form.houseNumber"></el-input>
            </el-form-item>
            <el-form-item label="身份证" required>
              <el-input v-model="form.idCard"></el-input>
            </el-form-item>
            <el-form-item label="性别" required>
              <el-radio-group v-model="form.gender">
                <el-radio label="男"></el-radio>
                <el-radio label="女"></el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="年龄">
              <el-input v-model="form.age"></el-input>
            </el-form-item>
            <el-form-item label="电话号码" required>
              <el-input v-model="form.phone"></el-input>
            </el-form-item>
            <el-form-item label="是否有高烧，咳嗽症状">
              <el-radio-group v-model="form.abnormal">
                <el-radio label="是"></el-radio>
                <el-radio label="否"></el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="是否为高危地区返回">
              <el-radio-group v-model="form.highRisk">
                <el-radio label="是"></el-radio>
                <el-radio label="否"></el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item class="form-box-btn">
              <el-button type="primary" @click="submit()">保存</el-button>
              <el-button @click="cancel()">取 消</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
    <el-dialog
      title="确定注销用户吗"
      :visible.sync="dialog"
      width="30%"
      center>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="loginOut()">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="如不填写基础信息则注册失败"
      :visible.sync="cancelDialog"
      width="30%"
      center>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="deleteUserfct()">确 定</el-button>
        <el-button @click="cancelDialog = true">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import FourMenu from '../../components/menu/four-menu'
import { insertBaseInfo, queryAllCommunity, queryEstateByCommunityId, deleteUser } from '../../api/apply-info'
export default {
  name: 'BasicInfo',
  components: { FourMenu },
  data () {
    return {
      userName: '',
      userId: '',
      centerDialogVisible: false,
      dialog: false,
      cancelDialog: false,
      form: {
        name: '',
        community: '',
        estate: '',
        houseNumber: '',
        idCard: '',
        gender: '',
        age: '',
        phone: '',
        abnormal: '',
        highRisk: ''
      },
      option: {},
      optionCommunity: [],
      optionEstate: [],
      insertData: {},
      index: 0,
      dataFourMenu: [],
      isSubmit: true
    }
  },
  mounted () {
    this.getUserName()
    this.getAllCommunity()
  },
  methods: {
    // 获取账户id
    getUserName () {
      this.userName = sessionStorage.getItem('userName')
      this.userId = sessionStorage.getItem('userId')
    },
    // 提交基础信息
    submit () {
      for (let j = 0; j < this.optionEstate.length; j++) {
        if (this.optionEstate[j].value === this.form.estate) {
          this.form.estate = this.optionEstate[j].label
        }
      }
      this.insertData = this.form
      if (this.form.gender === '男') {
        this.insertData.gender = 1
      } else if (this.form.gender === '女') {
        this.insertData.gender = 0
      }
      if (this.form.highRisk === '是') {
        this.insertData.highRisk = 1
      } else if (this.form.highRisk === '否') {
        this.insertData.highRisk = 0
      }
      if (this.form.abnormal === '是') {
        this.insertData.abnormal = 1
      } else if (this.form.abnormal === '否') {
        this.insertData.abnormal = 0
      }
      const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
      if (reg.test(this.form.idCard) === false) {
        this.$message('身份证不正确')
        this.isSubmit = false
      } else {
        if (this.form.phone.length !== 11) {
          this.$message('电话号码非法')
          this.isSubmit = false
        } else {
          this.isSubmit = true
        }
      }
      this.insertData.userId = this.userId
      console.log('this.insertData', this.insertData)
      if (this.isSubmit === true && this.form.name !== '' && this.form.community !== '' && this.form.estate !== '' && this.form.houseNumber !== '' && this.form.idCard !== '' && this.form.phone !== '') {
        insertBaseInfo(this.insertData).then(res => {
          console.log('insertBaseInfo res', res)
          this.$message(res.data.message)
          this.$router.push({ path: '/login' })
        })
      }
    },
    // 查询所有社区
    getAllCommunity () {
      queryAllCommunity().then(res => {
        console.log(res)
        for (let i = 0; i < res.data.data.length; i++) {
          // 把id和名字取出来 放进option里
          const label = res.data.data[i].communityName
          const value = res.data.data[i].id
          const option = { label, value }
          this.optionCommunity.push(option)
        }
      })
    },
    // 查询所属小区信息
    getEstate (val) {
      // 查找对应社区id的社区name
      for (let j = 0; j < this.optionCommunity.length; j++) {
        if (this.optionCommunity[j].value === val) {
          this.form.community = this.optionCommunity[j].label
        }
      }
      queryEstateByCommunityId(val).then(res => {
        for (let i = 0; i < res.data.data.length; i++) {
          // 把id和名字取出来 放进option里
          const label = res.data.data[i].estateName
          const value = res.data.data[i].id
          const option = { label, value }
          this.optionEstate.push(option)
        }
        console.log(this.optionEstate)
      })
    },
    // 取消填写删除用户
    cancel () {
      this.$confirm('取消填写基础信息，将注册失败', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUser(this.userId).then(res => {
          console.log('delete res', res)
          this.$router.push({ path: '/register' })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    }
  }
}
</script>
<style scoped>
.userName {
  margin-right: 10px;
  margin-left: 10px;
}
.loginOut-btn{
  float: right;
  margin-top: -5px;
  border-radius: 10px;
}
.box-card {
    margin-top: 10px;
    margin-left: 10%;
    width: 1200px;
    border-radius: 20px;
    background-color: rgba(255, 255, 255, 0.8);
}
.four-menu {
  margin-top: 10px;
  margin-bottom: 10px;
}
.form-btn {
  margin-left: 15%;
}
.form-box {
  width: 500px;
  margin-top: 10px;
  margin-left: 25%;
}
.form-box-btn {
  margin-left: 25%;
}
</style>
<style>
body{
  background-image: url('../../assets/bg3.jpeg');
  background-size: 100% 1000px;
  background-repeat: no-repeat;
}
.el-icon-switch-button {
  color: #e4252d;
  font-size: 17px;
  font-weight: bolder;
}
</style>
